package com.loongtech.bi.action.bigame;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.game.EntityNewUserCountry;
import com.loongtech.bi.manager.game.NewUserCountryManager;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.support.CommonGameParam;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 游戏内分析-国家分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/game/countryGame")
public class CountryGameBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 8 * Time.DAY));  // 前端传过来的开始时间
	private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 前端传过来的结束时间
	
	private List<NewUserCountryManager.CountryGameData> countryGameData;
	
	@Autowired
	NewUserCountryManager newUserCountryManager;

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);

            countryGameData = new ArrayList<>();

            List<EntityNewUserCountry> newUserCountryList = newUserCountryManager.getAllNewUserCountry(dateStart, dateEnd);

            if(newUserCountryList != null && newUserCountryList.size() > 0) {
                Map<Integer, NewUserCountryManager.CountryGameData> map = new LinkedHashMap<>();
                int allNewRole = 0;
                for (EntityNewUserCountry e : newUserCountryList) {
                    allNewRole += e.getUser_number();

                    int cid = e.getCountryid();
                    int oid = e.getOccupationid();
                    NewUserCountryManager.CountryGameData c = map.get(cid);
                    if(c == null) {
                        c = newUserCountryManager.new CountryGameData();
                        map.put(cid, c);
                    }

                    c.setCountryid(cid);
                    c.setCountryNewRole(c.getCountryNewRole() + e.getUser_number());
                    Map<Integer, Integer> l = c.getNewRoleSchoolCounts();
                    if(l == null) {
                        l = new LinkedHashMap<>();
                        c.setNewRoleSchoolCounts(l);
                    }
                    l.put(oid, (l.get(oid) == null ? 0 : l.get(oid)) + e.getUser_number());
                }

                for (Map.Entry<Integer, NewUserCountryManager.CountryGameData> entry : map.entrySet()) {
                    entry.getValue().setAllNewRole(allNewRole);
                    countryGameData.add(entry.getValue());
                }
            }

            result.put("res", "0");
            result.put("countryGameData", countryGameData);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}
	
	public String getSchoolNameById(int id) {
		return GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(String.valueOf(id)) == null ? id + "" : GameParamConfigManager.getItemsRevise(CommonGameParam.SCHOOL).get(String.valueOf(id)); //SchoolManager.getSchoolNameById(id);
	}

    public String getCountryNameById(int id) {
        return GameParamConfigManager.getItemsRevise(CommonGameParam.NATION_NAME).get(String.valueOf(id)) == null ? id + "" : GameParamConfigManager.getItemsRevise(CommonGameParam.NATION_NAME).get(String.valueOf(id)); //SchoolManager.getSchoolNameById(id);
    }

    @Override
    public void search() {

    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

    public List<NewUserCountryManager.CountryGameData> getCountryGameData() {
        return countryGameData;
    }

    public void setCountryGameData(List<NewUserCountryManager.CountryGameData> countryGameData) {
        this.countryGameData = countryGameData;
    }
}
